#include "sys.h"
static int log_fd;
static char* log_file;
int debug_level=0;

void log_init(char* filename){
	log_file = strdup(filename);
	log_fd =  open(log_file, O_CREAT|O_RDWR);
	if(log_fd ==-1){
		printf("open log file:%s failed! errno[%d]\n", filename, errno);	
	}
}
void  log_to_file(char* str, int size){
	int rc;
	rc = write(log_fd, str, size);
	

}


#define log_file(level, fmt, msg...)  \
		do{                    \
	    		if (level>= debug_level) {  \
				int size = 0;					\
				int off =0;					\
				char buf[4096];				\
				time_t timep; 					\
				struct timeval tv;				\
				struct timezone tz;				\
				pthread_t   tid;					\
				char* st =  NULL; 				\
							\
				gettimeofday (&tv , &tz);	\
				time(&timep); 				\
				st = ctime(&timep);			\
				st = strtok(st, "\n");		\
				tid = pthread_self();				\
				off = snprintf(buf+off,4096, "[%s:%d:%d][%u]%s:%d\t",  \
					st,tv.tv_sec,tv.tv_usec,tid, __FILE__,__LINE__);  \
				size +=off;					\
      	    			off = snprintf(buf+off,4096-size, fmt,##msg);       \
				size +=off;								\
      	    	log_to_file(buf,size);						\
    			}\
		}while(0)  

int main(){
		log_init("../test.log");
		log_file(1,"test");		
}
